﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PatrolTaskPointEditGrid.aspx.cs"
    Inherits="FantastPlatform.Decision.UI.PatrolTaskPointEditGrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        var orgPatrolTaskId = '<%=GetId() %>';
        var orgPatrolTaskRecoder;
        var url = '../Service/PatrolTaskViewService.aspx?Action=LoadPoints&Id=' + orgPatrolTaskId;
//        if (scheduleSquadronId > 0) {
//            url = 'GpsPatrol/Service/WorkSchedule.aspx?Action=SearchAddPoliceMan&SquadronCode=' + squadronCode + '&SquadronId=' + scheduleSquadronId;
//        }
        var ptpeg = new FantastPlatform.UI.PatrolTaskPointEditGrid({
            renderTo: 'ptpeg_div',
            url: url,
            width: detailWind.getInnerWidth(),
            height: detailWind.getInnerHeight(),
            tbar: [
             {
                 text: '保存',
                 iconCls: 'save',
                 id: 'save',
                 handler: function () { savePoint(detailWind, ptpeg); }

             }, '-', {
                text: '添加',
                iconCls: 'new_tab',
                id: 'addDetail',
                handler: function () { addPoint(ptpeg); }

            }, '-', {
                text: '移除',
                iconCls: 'delete',
                id: 'deleteDetail',
                handler: function () {
                    var node = checkGridSelected(ptpeg);
                    if (node) {
                        var id = '';
                        for (var i = 0; i < node.length; i++) {
                            if (node[i].get('Id') == 0) {
                                ptpeg.store.remove(node[i]);
                            } else {
                                id += node[i].get('Id') + ",";
                            }
                        }
                        id = id.substring(0, id.length - 1);
                        if (id && id != '' && id != null) {
                            Ext.Msg.confirm('提示', '确认要移除选中的数据吗？', function (btn) {
                                if (btn == 'yes') {
                                    Ext.Msg.wait('正在移除数据，请稍候……', '移除数据');
                                    //var fIds = getIdFromGrid(node, 'Id');
                                    function validateFeed(response, options) {
                                        var node = checkGridSelected(ptpeg);                
                                        for (var i = 0; i < node.length; i++) {
                                            ptpeg.store.remove(node[i]);
                                            savePoint(null, ptpeg);
                                        }
                                        var responseArray = Ext.util.JSON.decode(response.responseText);
                                        if (eval(responseArray.success)) {
                                            Ext.MessageBox.show({
                                                title: '操作提示',
                                                msg: responseArray.message,
                                                buttons: Ext.Msg.OK,
                                                icon: Ext.MessageBox.INFO,
                                                miniWidth: 200
                                            });                                          
                                        }
                                    }
                                    ajaxCalled(null, '../Service/PatrolTaskViewService.aspx?Action=Delete', id, null, validateFeed);
                                }

                            })
                        }
                    }
                }
            }]
        });
        ptpeg.onUpdataClick(orgPatrolTaskRecoder);
      
        function addPoint(grid) {
            var MyRecord = new Ext.data.Record.create([
                     { name: 'Id', type: 'int' },
                     { name: 'Name', type: 'string' },
                     { name: 'SelfOrgPersonNum', type: 'int' },
                     { name: 'SelfOrgCar', type: 'int' },
                     { name: 'OtherOrgName', type: 'string' },
                     { name: 'OtherOrgPersonNum', type: 'int' }                    
                ]);
            var count = grid.store.getCount();
            var p = new MyRecord({
                Id: 0,
                Name: '',               
                SelfOrgPersonNum: 0,
                SelfOrgCar: 0,
                OtherOrgName: '',
                OtherOrgPersonNum: 0
            })
            grid.store.insert(count, p);      
        }
        function savePoint(sender, grid) {
            function encodeStore() {
                var records;
                if (grid != null) {
                    records = grid.store.getRange();
                }
                var dataStr = '[';
                for (var i = 0; i < records.length; i++) {
                    var orgName = "";
                    if (records[i].get("OtherOrgName") != null) {
                        orgName = records[i].get("OtherOrgName");
                    }
                    dataStr += '{"Id":"' + records[i].get("Id") + '","Name":"' + records[i].get("Name") + '","SelfOrgPersonNum":"' + records[i].get("SelfOrgPersonNum") + '","SelfOrgCar":"' + records[i].get("SelfOrgCar") + '","OtherOrgName":"' + orgName + '","OtherOrgPersonNum":"' + records[i].get("OtherOrgPersonNum") + '"},';
                }
                if (dataStr && records.length != 0) {
                    dataStr = dataStr.substring(0, dataStr.length - 1);
                }
                dataStr += "]";
                return dataStr;
            };
            orgPatrolTaskRecoder.Points = encodeStore(); 
            if (sender) {
                sender.close();
            }
        }
        function addDescription(scheduleSquadronId) {
            if (scheduleSquadronId > 0) {
                var id = GUID();
                descriptionWind = openWind("GpsPatrol/AddDetailDescription.aspx?id=" + scheduleSquadronId, id, '添加备注', 300, 160, 'new_tab');
            }
            else
                showErrorMessage("操作失败", "该警员未添加到此次排班", "");
        }
    </script>
</head>
<body>
    <div id="ptpeg_div">
    </div>
</body>
</html>
